package customfields;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Graphics;
import util.LookAndFeel;

public class FolderField extends Field{
	
	private String text;
	private int height;
	private int width;
	private int padding = 20;
	private int textColorUnfocused = Color.BLACK;
	private int textColorFocused = Color.WHITE;
	private int textColor;
	private Bitmap folder = Bitmap.getBitmapResource(LookAndFeel.icono_carpeta);

	public FolderField(String name, long style){
		super(style);
		this.text = name;
		height = (int) (getFont().getHeight()*2);  
		width = (int) (Display.getWidth()*0.9); 
	}

	public FolderField(String txt, long style, int w, int h){
		super(style);
		this.text = txt;
		height = w;  
		width = h; 
	}

	public FolderField(String txt){
		super();
		this.text=txt;
		height = (int) (getFont().getHeight()*1.5);  
		width = (int) (Display.getWidth()*0.9); 
	}

	public int getPreferredWidth() {  
		return width;  
	}  

	public int getPreferredHeight() {  
		return height;  
	}  

	protected void onFocus(int direction) {
		textColor = textColorFocused; 
		invalidate();  
	}  

	protected void onUnfocus() {  
		textColor = textColorUnfocused;  
		invalidate();  
	} 

	protected void layout(int arg0, int arg1) {  

		setExtent(getPreferredWidth(), getPreferredHeight());  
	}  

	protected void paint(Graphics graphics) {
		// draw folder icon
		graphics.drawBitmap(10,height-padding-30/2,30,30,folder,0,0);
		// draw text: left aligned
		if(getFont().getAdvance(text)>getWidth()){
			text = dot(text);
		}
		graphics.setColor(textColor);
		graphics.drawText(text,padding+30,padding/2);
	}
	
	private String dot(String word){
		word = word.substring(0,word.length()-1);
		while (getFont().getAdvance(word+" (...)")>(getWidth()-60)){
			word = word.substring(0,word.length()-1);
		}
		return word+" (...)";
	}
	
}
